package org.ttitfly.workflow.factory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 对象工厂
 * 
 * @author 潘海春
 * @version 2008-12-28
 */
public class DefaultObjectFactory implements ObjectFactory {

	/**
	 * 对象生成器
	 */
	private ObjectCreator creator;

	/**
	 * 对象缓存
	 */
	private Map<String, Object> cache = new ConcurrentHashMap<String, Object>();

	/**
	 * 构造函数
	 * 
	 * @param creator
	 */
	public DefaultObjectFactory(ObjectCreator creator) {
		this.creator = creator;
	}

	@Override
	public void bindBean(String name, Object bean) {
		cache.put(name, bean);
	}

	@Override
	public Object getBean(String name) throws ObjectNameUnavailableException {
		Object object = cache.get(name);
		if (object == null) {
			object = creator.createObject(name);
			bindBean(name, object);
		}
		return object;
	}

	@Override
	public void registBean(String name, String className) throws ObjectNameUnavailableException {
		bindBean(name, creator.createObject(className));
	}

}
